package cn.tedu.web.greed.servlet;

import cn.tedu.web.greed.JdbcUtils;
import org.apache.commons.lang3.RandomStringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(urlPatterns = "/goods/insert")
@MultipartConfig(
        location = "C:\\Users\\86151\\Desktop\\web\\upload-images",
        maxFileSize = 100 * 1024 * 1024,
        maxRequestSize = 100 * 1024 * 1024
)

public class GoodsInsertServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        Part file = req.getPart("i");
        //2.保存文件
        //
        String s = RandomStringUtils.randomAlphanumeric(5);
        long l = System.currentTimeMillis();
        String sl = s + l + ".png";
        file.write(sl);
        //3.修改数据库
        String name = req.getParameter("n");
        //
        String price = req.getParameter("p");

        String sql = "INSERT INTO goods (name,price,image) VALUES (?,?,?)";
        JdbcUtils.modify(sql, name, price,sl);
        //4.跳转到商品列表页面
        resp.sendRedirect("/greed/goods-list.jsp");
    }
}
